home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BTSETCUR.C < prev    next >
Text File  |  1991-09-23  |  3KB  |  117 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btsetcur.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STRING
  11. #include <string.h>
  12. #endif
  13.  
  14. /* library headers */
  15. #include <blkio.h>
  16.  
  17. /* local headers */
  18. #include "btree_.h"
  19.  
  20. /*man---------------------------------------------------------------------------
  21. NAME
  22.      btsetcur - set btree cursor
  23.  
  24. SYNOPSIS
  25.      #include <btree.h>
  26.  
  27.      int btsetcur(btp, btposp)
  28.      btree_t *btp;
  29.      const btpos_t *btposp;
  30.  
  31. DESCRIPTION
  32.      The btsetcur function sets the current cursor position for btree
  33.      btp to the value in btposp.  btposp should point to a cursor
  34.      value saved previously with btgetcur.  If btposp is the NULL
  35.      pointer, the cursor is set to null.  It is important to remember
  36.      that a btree position is not valid after an insertion, deletion,
  37.      or unlock.
  38.  
  39.      btsetcur will fail if one or more of the following is true:
  40.  
  41.      [EINVAL]       btp is not a valid btree pointer.
  42.      [BTELOCK]      btp is not locked.
  43.      [BTENKEY]      btposp points to an invalid cursor value.
  44.      [BTENOPEN]     btp is not open.
  45.  
  46. SEE ALSO
  47.      btgetcur.
  48.  
  49. DIAGNOSTICS
  50.      Upon successful completion, a value of 0 is returned.  Otherwise,
  51.      a value of -1 is returned, and errno set to indicate the error.
  52.  
  53. ------------------------------------------------------------------------------*/
  54. #ifdef AC_PROTO
  55. int btsetcur(btree_t *btp, const btpos_t *btposp)
  56. #else
  57. int btsetcur(btp, btposp)
  58. btree_t *btp;
  59. const btpos_t *btposp;
  60. #endif
  61. {
  62.     /* validate arguments */
  63.     if (!bt_valid(btp)) {
  64.         errno = EINVAL;
  65.         return -1;
  66.     }
  67.  
  68.     /* check if not open */
  69.     if (!(btp->flags & BTOPEN)) {
  70.         errno = BTENOPEN;
  71.         return -1;
  72.     }
  73.  
  74.     /* check locks */
  75.     if (!(btp->flags & BTLOCKS)) {
  76.         errno = BTELOCK;
  77.         return -1;
  78.     }
  79.  
  80.     /* set cursor */
  81.     if (btposp == NULL) {
  82.         btp->cbtpos.node = NIL;        /* set cursor to null */
  83.         btp->cbtpos.key = 0;
  84.     } else {
  85.         memcpy(&btp->cbtpos, btposp, sizeof(btp->cbtpos));
  86.     }
  87.  
  88.     /* read in new current node */
  89.     if (btp->cbtpos.node == NIL) {
  90.         bt_ndinit(btp, btp->cbtnp);
  91.     } else {
  92.         if (bt_ndget(btp, btp->cbtpos.node, btp->cbtnp) == -1) {
  93.             BTEPRINT;
  94.             if (errno == BEEOF) errno = BTENKEY;
  95.             return -1;
  96.         }
  97.         if (btp->cbtnp->n == 0) {    /* check if block empty */
  98.             btp->cbtpos.node = NIL;
  99.             btp->cbtpos.key = 0;
  100.             bt_ndinit(btp, btp->cbtnp);
  101.             errno = BTENKEY;
  102.             return -1;
  103.         }
  104.     }
  105.  
  106.     /* check if key number in range */
  107.     if (btp->cbtpos.key > btp->cbtnp->n) {
  108.         btp->cbtpos.node = NIL;
  109.         btp->cbtpos.key = 0;
  110.         bt_ndinit(btp, btp->cbtnp);
  111.         errno = BTENKEY;
  112.         return -1;
  113.     }
  114.  
  115.     return 0;
  116. }
  117.